'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    // ctx.body = 'hi, egg';
    await ctx.render('index/index')
  }
  async ciku(){

  	const { ctx } = this;
  	await ctx.render('/index/ciku')
  }
  async ketang(){
  	const { ctx } = this;
  	await ctx.render('/index/ketang')

  }
  async ketang1(){
    const { ctx } = this;
    const result = await ctx.service.user.getpinglun()
    await ctx.render('/index/ketang1',{result})

  }
  async youxi(){
  	const { ctx } = this;
  	await ctx.render('/index/youxi')
  }
  async login(){
  	const { ctx } = this;
  	await ctx.render('/index/login',{login:true})
  }
  async register(){
  	const { ctx } = this;
  	await ctx.render('/index/login',{login:false})
  }
  async zhuce(){
    const { ctx } = this;
    const { phone , password} = ctx.request.body
    console.log({name:phone,password})
    const result = await ctx.service.user.addUser({name:phone,password})
    if(result){
      ctx.body = {
        code:0,
        msg:'注册成功'
      }
    }
    
  }
  async me(){
  	const { ctx } = this;
  	await ctx.render('/index/me')
  }
  async denglu(){
    const { ctx } = this;
    const { name,password } = ctx.request.body
    const result = await ctx.service.user.user({name})
    console.log(result)
    if(result){
      ctx.session.username = result
      ctx.session.maxAge = 1000*60*60
      ctx.body = {
        code:0,
        msg:'登录成功'
      }
    }else{
      ctx.body = {
        code:1,
        msg:'用户名或密码不正确'
      }
    }
  }
  async tui(){
    const { ctx } = this;
    ctx.session = null
    ctx.redirect('/login')
  }
  async fabu(){

    const { ctx } = this;
    const { content } = ctx.request.body
    
    function datez(){
      var now = new Date();
      var year = now.getFullYear(); //得到年份
      var month = now.getMonth();//得到月份
      var date = now.getDate();//得到日期
      var day = now.getDay();//得到周几
      var hour = now.getHours();//得到小时
      var minu = now.getMinutes();//得到分钟
      var sec = now.getSeconds();//得到秒
      var time = year + "年" + month + "月" + date + "日" + " " + hour + ":" + minu + ":" + sec;
      return time
    }
    if(ctx.session.username){
      const result = await ctx.service.user.fabu({content,time:datez(),username:ctx.session.username.name})
      if(result){
        ctx.body = {
          code:0,
          msg:'发布成功！'
        }
      }
    }else{
      ctx.body = {
        code:1,
        msg:'登录后才能发布评论！'
      }
    }
    // return
    
  }
}

module.exports = HomeController;
